﻿namespace AntdUI_HamburgerMenuTabs.Pages
{
    public partial class Settings : UserControl
    {
        public Settings(FrmMain? frmMain)
        {
            InitializeComponent();

            btnClose.Click += (s, e) =>
            {
                Dispose();
            };

            collapse1.ButtonClick += (s, e) =>
            {
                if (e.Value.Name == "btnReload")
                {
                    collapseItemOrther.Expand = true;
                    FlowLayoutDemo();
                }
            };

            #region Esc 退出

            switchEscQuit.Checked = Properties.Settings.Default.EscQuit;

            switchEscQuit.CheckedChanged += (s, e) =>
            {
                Properties.Settings.Default.EscQuit = e.Value;
                Properties.Settings.Default.Save();
            };

            #endregion

            #region 过渡动画

            switchAnimation.Checked = Properties.Settings.Default.Animation;
            AntdUI.Config.Animation = Properties.Settings.Default.Animation;

            switchAnimation.CheckedChanged += (s, e) =>
            {
                AntdUI.Config.Animation = e.Value;
                Properties.Settings.Default.Animation = e.Value;
                Properties.Settings.Default.Save();
            };

            #endregion

            #region 页面模式

            switchPageMode.Checked = Properties.Settings.Default.MultiTabs;

            switchPageMode.CheckedChanged += (s, e) =>
            {
                frmMain?.InitHm(e.Value);
                Properties.Settings.Default.MultiTabs = e.Value;
                Properties.Settings.Default.Save();
            };

            #endregion

            #region 记住访问页

            switchRememberLastPage.Checked = Properties.Settings.Default.RememberLastPage;

            switchRememberLastPage.CheckedChanged += (s, e) =>
            {
                Properties.Settings.Default.RememberLastPage = e.Value;
                Properties.Settings.Default.Save();
            };

            #endregion

            #region 侧边栏菜单手风琴模式

            switchAccordion.Checked = Properties.Settings.Default.Accordion;

            switchAccordion.CheckedChanged += (s, e) =>
            {
                frmMain?.SetAccordion(e.Value);
                Properties.Settings.Default.Accordion = e.Value;
                Properties.Settings.Default.Save();
            };

            #endregion

            #region 页面全屏覆盖任务栏

            switchPageFullScreenMode.Checked = Properties.Settings.Default.PageFullScreenMode;

            switchPageFullScreenMode.CheckedChanged += (s, e) =>
            {
                Properties.Settings.Default.PageFullScreenMode = e.Value;
                Properties.Settings.Default.Save();
            };

            #endregion
        }

        /// <summary>
        /// 全局快捷键
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Dispose();
                    break;
            }
            return false;
        }

        /// <summary>
        /// 添加示例选项（流式布局）
        /// </summary>
        private void FlowLayoutDemo()
        {
            stackPanelOrther.SuspendLayout();

            stackPanelOrther.Controls.Clear();

            for (int i = 0; i < 20; i++)
            {
                AntdUI.Button btn = new()
                {
                    Name = $"btn{i + 1}",
                    Text = $"按钮{i + 1:000}",
                    Type = AntdUI.TTypeMini.Primary,
                    Height = 50,
                    Dock = DockStyle.Top,
                };
                stackPanelOrther.Controls.Add(btn);
                btn.BringToFront();
            }

            stackPanelOrther.ResumeLayout();
        }
    }
}